const resolve = require('path').resolve
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const url = require('url')

const express = require('express')
const bodyParser = require('body-parser')
const apiRoutes = express.Router()
const devRequest = require('./src/api/devRequest')
const { remotePathPrefix } = require('./deployment.conf.js')
const os = require('os')
var ifaces = os.networkInterfaces();
var ip = ''
for (var dev in ifaces) {
    var alias = 0;
    ifaces[dev].forEach(function (details) {
        if (details.family == 'IPv4' && dev == 'WLAN') {
            ip = details.address
            ++alias;

        }
    });
}
console.log('ip:' + ip)


const publicPath = `https://bcy-h5.oss-cn-hangzhou.aliyuncs.com/${remotePathPrefix}/`

module.exports = (options = {}) => ({
    entry: {
        vendor: './src/vendor',
        index: './src/main.js'
    },
    output: {
        path: resolve(__dirname, 'dist'),
        filename: options.dev ? '[name].js' : '[name].js?[chunkhash]',
        chunkFilename: '[id].js?[chunkhash]',
        publicPath: options.dev ? '/assets/' : publicPath
    },
    module: {
        rules: [{
            test: /\.vue$/,
            use: ['vue-loader']
        },
        {
            test: /\.js$/,
            use: ['babel-loader'],
            exclude: /node_modules/
        },
        {
            test: /\.css$/,
            use: ['style-loader', 'css-loader', 'postcss-loader']
        },
        {
            test: /\.(png|jpg|jpeg|gif|eot|ttf|woff|woff2|svg|svgz)(\?.+)?$/,
            use: [{
                loader: 'url-loader',
                options: {
                    limit: 10000
                }
            }]
        },
        {
            test: /\.less$/,
            loader: "style-loader!css-loader!less-loader"
        }
        ]
    },
    plugins: [
        new webpack.optimize.CommonsChunkPlugin({
            names: ['vendor', 'manifest']
        }),
        new HtmlWebpackPlugin({
            template: 'src/index.html'
        }),
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery"
        }),
        new webpack.DefinePlugin({
            'process.env': require('./config/dev.env')
        }),
    ],
    externals: {},
    resolve: {
        alias: {
            '~': resolve(__dirname, 'src')
        },
        extensions: ['.js', '.vue', '.json', '.css']
    },
    devServer: {
        host: ip || '192.168.10.139',
        port: 8010,
        proxy: {
            '/api/': {
                target: `http://${ip || '192.168.10.139'}:8010`,
                changeOrigin: true,
                pathRewrite: {
                    '^/api': ''
                }
            }
        },
        historyApiFallback: {
            index: url.parse(options.dev ? '/assets/' : publicPath).pathname
        },
        before(apiRoutes) {
            const urlencodedParser = bodyParser.urlencoded({ extended: false })
            apiRoutes.post('/worker/person', urlencodedParser, devRequest.workerPerson)
            apiRoutes.post('/wechat/webEmpower/jsConfig', urlencodedParser, devRequest.shareConfig)
            apiRoutes.post('/worker/jobList', urlencodedParser, devRequest.job)
            apiRoutes.get('/tag', urlencodedParser, devRequest.tag)
            apiRoutes.post('/company/describe', urlencodedParser, devRequest.describe)
            apiRoutes.get('/industry', urlencodedParser, devRequest.industry)
            apiRoutes.get('/area', urlencodedParser, devRequest.area)
            apiRoutes.post('/worker/job/star', urlencodedParser, devRequest.star)
            apiRoutes.get('/worker/job/show', devRequest.jobDetail)
            apiRoutes.post('/worker/company', urlencodedParser, devRequest.company)
            apiRoutes.post('/worker/companyList', urlencodedParser, devRequest.companyList)
            apiRoutes.post('/worker/show', urlencodedParser, devRequest.resumeShow)
            apiRoutes.post('/worker/showDemand', urlencodedParser, devRequest.showDemand)
            apiRoutes.post('/worker/me', urlencodedParser, devRequest.wokerMe)
            apiRoutes.post('/company/me', urlencodedParser, devRequest.companyMe)
            apiRoutes.post('/worker/job/starred', urlencodedParser, devRequest.workerStarred)
            apiRoutes.post('/worker/job/applied', urlencodedParser, devRequest.workerApplied)
            apiRoutes.post('/position', urlencodedParser, devRequest.positionList)
            apiRoutes.post('/worker/demand', urlencodedParser, devRequest.editDemand)
            apiRoutes.post('/worker/resume', urlencodedParser, devRequest.editResume)
            apiRoutes.post('/company/company/license', urlencodedParser, devRequest.license)
            apiRoutes.post('/company/company/showLicense', urlencodedParser, devRequest.showLicense)
            apiRoutes.post('/company/resume', urlencodedParser, devRequest.companyResume)
            apiRoutes.post('/company/resume/item', urlencodedParser, devRequest.companyResumeItem)
            apiRoutes.post('/company/resume/refuse', urlencodedParser, devRequest.refuseResume)
            apiRoutes.post('/company/resume/call', urlencodedParser, devRequest.callResume)
            apiRoutes.post('/company/company/show', urlencodedParser, devRequest.companyShow)
            apiRoutes.post('/company/job/list', urlencodedParser, devRequest.companyJobList)
            apiRoutes.post('/company/job/describe', urlencodedParser, devRequest.editJob)
            apiRoutes.post('/company/job/show', urlencodedParser, devRequest.companyjobDetail)
            apiRoutes.post('/company/job/offline', urlencodedParser, devRequest.companyjobDetail)
            apiRoutes.post('/worker/job/apply', urlencodedParser, devRequest.jobApply)
            apiRoutes.post('/company/company/list', urlencodedParser, devRequest.myCompany)
            apiRoutes.post('/worker/wallet', urlencodedParser, devRequest.workerWallet)
            apiRoutes.post('/worker/wallet/log', urlencodedParser, devRequest.workerWalletLog)
            apiRoutes.post('/company/resume/buy', urlencodedParser, devRequest.buyResume)
            apiRoutes.post('/worker/job/complaint', urlencodedParser, devRequest.complaint)
            apiRoutes.post('/company/resume/order', urlencodedParser, devRequest.resumeOrder)
            apiRoutes.post('/company/wallet/log', urlencodedParser, devRequest.walletLog)
            apiRoutes.post('/index/resolveSaas', urlencodedParser, devRequest.getSaas)
            apiRoutes.get('/worker/apply-withdraw', urlencodedParser, devRequest.applyWithdraw)
            apiRoutes.post('/worker/resumev2', urlencodedParser, devRequest.resumev2)
            apiRoutes.post('/poster/companyPoster', urlencodedParser, devRequest.CompanyPoster)
            apiRoutes.post('/poster/JobPoster', urlencodedParser, devRequest.JobPoster)
            apiRoutes.post('/worker/protocol', urlencodedParser, devRequest.protocol)
            apiRoutes.post('/company/job/resumePoolCategory', urlencodedParser, devRequest.resumePoolCategory)
            apiRoutes.post('/company/job/resumePool', urlencodedParser, devRequest.resumePool)
            //phoneLogin.js
            apiRoutes.post('/login', urlencodedParser, devRequest.phoneLogin)
            apiRoutes.post('/login/wechatMPAuthorize', urlencodedParser, devRequest.userInfo)
            apiRoutes.post('/company/job/topList', urlencodedParser, devRequest.topList)
            apiRoutes.post('/company/job/buyTopList', urlencodedParser, devRequest.buyTopList)
            apiRoutes.post('/wap/user/verifyPhone', urlencodedParser, devRequest.verifyPhone)
            apiRoutes.get('/oss/sign', urlencodedParser, devRequest.ossSign)
            apiRoutes.post('/company/wallet/recharge/:aid', urlencodedParser, devRequest.recharge)
            apiRoutes.post('/company/member/memberList', urlencodedParser, devRequest.memberList)
            apiRoutes.post('/company/member/buyMember', urlencodedParser, devRequest.buyMember)
            apiRoutes.post('/company/member/buyMemberMenu', urlencodedParser, devRequest.buyMemberMenu)
        }
    },
    devtool: options.dev ? '#eval-source-map' : '#source-map'
})